package com.snap.vseries.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.BuildConfig;
import com.snap.vseries.MoleApplication;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.framework.MoleExecutor;
import com.snap.vseries.framework.MoleTask;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.ApkUtils;
import com.snap.vseries.utils.SpUtils;
import com.snap.vseries.utils.ToastUtils;
import com.snap.vseries.utils.TopTitleUtils;
import com.snap.vseries.utils.ZipUtil;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GeneralActivity extends BaseActivity {
    public static final int NO_USB = 999;
    public static final int SUCCESS = 1;
    public static final int FAIL = 0;
    @BindView(R.id.export_log_tv)
    TextButton expLogTv;
    @BindView(R.id.firmware_version_tv)
    TextView firmwareVersionTv;
    @BindView(R.id.serial_number_tv)
    TextView serialNumberTv;
    private Dialog dialog;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, GeneralActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_general;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        new TopTitleUtils(this).setTitle("通用设置");
        serialNumberTv.setText(AppConfig.USER_ID);
        firmwareVersionTv.setText(String.format("v%s", BuildConfig.VERSION_NAME));
    }

    private void expLog() {
        showLoading("日志导出中...");
        export();
    }

    private void showLoading(String content) {
        dialog = new Dialog(this, R.style.DimDialogStyle);
        dialog.setContentView(R.layout.dialog_loading);
        TextView contentTv = dialog.findViewById(R.id.content_tv);
        contentTv.setText(content);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.show();
    }

    public void clearCache(View view) {
        new DialogView(GeneralActivity.this)
                .setTitle("恢复默认设置")
                .setTextContent("确认恢复默认设置？")
                .setOnClickListener(new DialogView.OnSureListener() {
                    @Override
                    public void onSureClick(String content) {
                        SpUtils.get().clearAll();
                        ToastUtils.get().showText("恢复默认设置成功");
                    }
                }).show();
    }

    private void export() {
        MoleExecutor.getInstance().execute(new MoleTask<Integer>() {
            @Override
            public Integer runInBackground() {
                boolean isSuccess;
                List<String> pathList = ApkUtils.getUSBPaths(MoleApplication.get());
                if (pathList != null && pathList.size() != 0) {
                    String usbPath = pathList.get(0) + File.separator;
                    isSuccess = ZipUtil.exportFile(usbPath);
                    if (isSuccess) {
                        return SUCCESS;
                    } else {
                        return FAIL;
                    }
                } else {
                    return NO_USB;
                }
            }

            @Override
            public void onCompleted(Integer result) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                switch (result) {
                    case SUCCESS:
                        ToastUtils.get().showText("文件导出成功");
                        break;
                    case FAIL:
                        ToastUtils.get().showText("文件导出失败");
                        break;
                    case NO_USB:
                        ToastUtils.get().showText("未检测到U盘");
                        break;
                    default:
                }
            }
        });
    }

    @OnClick({R.id.export_log_tv, R.id.warn_temp_fl,R.id.firmware_update_tv, R.id.contact_tv,R.id.amp_setting_ll, R.id.amp_cross_ll})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.export_log_tv:
                expLog();
                break;
            case R.id.warn_temp_fl:
                //警戒温度设置
                ToastUtils.get().showText("尚未添加该功能");
                break;
            case R.id.firmware_update_tv:
                //app检查更新升级
                ApkUtils.checkUpdate(this);
                break;
            case R.id.contact_tv:
                //关于本机
                break;
            case R.id.amp_setting_ll:
                //荧光较准
                Intent settingIntent = new Intent(this, AmpCalibrationActivity.class);
                startActivity(settingIntent);
                break;
            case R.id.amp_cross_ll:
                //荧光串扰
                Intent intent = new Intent(this, AmpCrossActivity.class);
                startActivity(intent);
                break;
            default:
        }
    }

}
